﻿using System;
using System.Collections.Generic;
using System.Text;

namespace KBEngine.Common
{
    public class ComponentStatus : ServerApp
    {
        ComponentType _componentType;
        List<CSData> _cSData;
        public List<CSData> CSDataList { get => _cSData; }
        public ComponentStatus(ComponentType componentType)
        {
            registerMsg(Define.CONSOLE_PROFILECB_MSGID, onComponentStatusMsg);
            _componentType = componentType;
            _cSData = new List<CSData>();
            System.Diagnostics.Debug.Assert(Array.Exists(Enum.GetNames(typeof(CMD_ID_queryAppsLoads)), _componentType.ToString().Equals));
        }

        public void clearCSData()
        {
            _cSData.Clear();
        }

        public void requireQueryCS()
        {
            var queryAppsLoadsID = (ushort)Enum.Parse(typeof(CMD_ID_queryAppsLoads), _componentType.ToString());
            Bundle bundle = Bundle.createObject();
            bundle.newMessage(FixedMsg.Get(queryAppsLoadsID));
            send(bundle);
        }

        private void onComponentStatusMsg(MemoryStream ms)
        {
            if (_componentType == ComponentType.baseappmgr)
            {
                while (!ms.readEOF())
                {
                    var csData = new CSData();
                    csData.componentID = ms.readUint64();
                    csData.load = ms.readFloat();
                    csData.numBases = ms.readInt32();
                    csData.numEntities = ms.readInt32();
                    csData.numProxices = ms.readInt32();
                    csData.flags = ms.readUint32();
                    _cSData.Add(csData);
                }
            }
            else
            {
                while (!ms.readEOF())
                {
                    var csData = new CSData();
                    csData.componentID = ms.readUint64();
                    csData.load = ms.readFloat();
                    csData.numEntities = ms.readInt32();
                    csData.flags = ms.readUint32();
                    _cSData.Add(csData);
                }
            }
        }

        public class CSData
        {
            public ulong componentID;
            public float load;
            public int numEntities;
            public uint flags;

            //baseappmgr才有
            public int numBases;
            public int numProxices;
        }

        private enum CMD_ID_queryAppsLoads : ushort
        {
            baseappmgr = 50001,
            cellappmgr = 50002,
        }
    }
}
